<?php
include('class/class_connect.php');
include('class/class_page.php');
include('class/city_rus.php');
include('class/class_form.php');
include('class/class_checkGetData.php');
include('includes/rubriks.php');
foreach($_GET as $key=>$val)
{
	$_GET[$key]=checkGetData::safetyCheck($val);
}
foreach($_POST as $key=>$val)
{
	if($key=='cityId')continue;
	$_POST[$key]=checkGetData::safetyCheck($val);
}
$aCheckData=new checkGetData('main');
$aCheckData->checkTableId('nets_news', $_GET['id']);
session_start();
if(!$_SESSION['login'])
{
	header('location: registr.php?city='.$_GET['city']);
	exit;
}
$aConnect=new connect('main');
$row=mysql_fetch_array($aConnect->select_all('nets_news', "WHERE id='".$_GET['id']."'"));
$r=mysql_fetch_array($aConnect->select_all('nets', "WHERE id='".$row['nets_id']."'"));
if(strtolower($r['user'])!=strtolower($_SESSION['login']))
{
	echo ('
	<center>
		<h1>У Вас нет доступа к запрашиваемой странице!</h1>
		<a href="index.php">На главную</a>
		</center>
	');
	exit;
}
$aConnect=new connect('main');
$aForm=new form();
$warning=false;
if($_POST['undoFirms'])unset($_POST);
if($_POST['editNews'])
{
	$warning=false;
	if($_POST['name'])
	{
		$warning=$aForm->checkInput($_POST['name'], 'Заголовок', 'name', $row['name'], 80, 3, "(.)", "", '');
		if(!$warning)$data['name']=$_POST['name'];
	}
	if($_POST['stext'] && !$warning)
	{
		$warning=$aForm->checkInput($_POST['stext'], 'Текст', 'stext', $row['stext'], 1000, 10, "(.)", "", '');
		if(!$warning)$data['stext']=$_POST['stext'];
	}
	//$data['data']=date('Y-m-d H:i:s');
	if(!$warning)
	{
		if($data)//сделаны изменения
		{
			foreach($data as $r=>$v)
			{
				$dataTo[]=$r."='".$v."'";
			}
			if($dataTo)$dataToUpdate=implode(', ', $dataTo);
		}
		if($dataToUpdate)
			$aConnect->update_data('nets_news', $dataToUpdate, "WHERE id='".$_GET['id']."'"); //обновляем данные фирмы
		if($_POST['imgName'])
		{
			if(!file_exists('pictures/netsnewspics/'))
				mkdir('pictures/netsnewspics/', 0777, true);
			if(file_exists($_POST['imgName'].'.jpg'))
			{
				copy($_POST['imgName'].'.jpg', 'pictures/netsnewspics/'.$_GET['id'].'.jpg');
			}
		}
		$aConnect=new connect('fn');
		for($i=0; $i<count($_POST['cityId']); $i++)
		{
			$cityId=explode('^', $_POST['cityId'][$i]);
			$city=$cityId[0];
			$id=$cityId[1];
			if($dataToUpdate)
				$aConnect->update_data($city, $dataToUpdate, "WHERE id='".$id."'");
			if(!file_exists('pictures/firmsnewspics/'.$city.'/'.$id.'/'))
				mkdir('pictures/firmsnewspics/'.$city.'/'.$id.'/', 0777, true);
			if(file_exists($_POST['imgName'].'.jpg'))
			{
				copy($_POST['imgName'].'.jpg', 'pictures/firmsnewspics/'.$city.'/'.$id.'.jpg');
			}

		}
		header('location: netscabinet.php?city='.$_GET['city'].'&id='.$row['nets_id']);
	}
}
if($_POST['deleteNews'])
{
	$aConnect=new connect('main');
	$query="DELETE from nets_news WHERE id='".$_GET['id']."'";
	mysql_query($query) or die(mysql_error());
	if(file_exists('pictures/netsnewspics/'.$_GET['id'].'.jpg'))
		unlink('pictures/netsnewspics/'.$_GET['id'].'.jpg');
	for($i=0; $i<count($_POST['cityId']); $i++)
	{
		$cityId=explode('^', $_POST['cityId'][$i]);
		$city=$cityId[0];
		$id=$cityId[1];
		$aConnect=new connect('fn');
		$query="DELETE from $city WHERE id='".$id."'";
		mysql_query($query) or die(mysql_error());
		if(file_exists('pictures/firmsnewspics/'.$city.'/'.$id.'.jpg'))
			unlink('pictures/firmsnewspics/'.$city.'/'.$id.'.jpg');
	}
	header('location: netscabinet.php?city='.$_GET['city'].'&id='.$row['nets_id']);

}
$aConnect=new connect('main');
$row=mysql_fetch_array($aConnect->select_all('nets_news', "WHERE id='".$_GET['id']."'"));
$aForm=new  form();
class aFirmPage extends page
{
	var $warning;
	var $row=array();
	function __construct($warning, $row=array(), $r=array())
	{
		$this->warning=$warning;
		$this->row=$row;
		$this->r=$r;
		parent::__construct();
	}
	function title()
	{
		echo 'Редактировать Новость';
	}
	function maincontent()
	{
		$cityRus=new city_rus_class();
		$aForm=new  form();
		$aForm->includeStyle();
		$aForm->includeScript();
		$imgName='temp'.rand();
		if(file_exists('pictures/netsnewspics/'.$this->row['id'].'.jpg'))
			 $avatar='pictures/netsnewspics/'.$this->row['id'].'.jpg';
		if($_POST['imgName'])
		{
			$avatar=$_POST['imgName'].'.jpg';
		}
		echo ('
		<script type="text/javascript" src="scripts/editNews.js">
		</script>
		<script type="text/javascript" src="jquery/ajaxUpload.js">
  		</script>
		<link rel="stylesheet" type="text/css" href="style/editNews.css" />
		<div class="route">
			<div class="routeEl">
				<a href="pcabinet.php?city='.$_GET['city'].'">Личный кабинет</a>
			</div>
			<div class="routeEl">
				<a href="my_ness.php?city='.$_GET['city'].'">Мои сети</a>
			</div>
			<div class="routeEl">
				<a href="netscabinet.php?city='.$_GET['city'].'&id='.$this->row['nets_id'].'">'.$this->row['nets_name'].'</a>
			</div>
			<div class="routeEl">
				Редактировать новость
			</div>
		</div>
		  <div class="form" align="center">
		  <h3>Редактировать Новость</h3>
		   <form method="post" id="editNews" name="editNews" action="" autocomplete="off">
		   		<input name="city" type="hidden" value="'.$_GET['city'].'" id="city">
		   		<center id="warning">&nbsp;'.$this->warning.'</center>
		   		<div class="myform">
			   		<div class="signature">
			   	 		Изображение:
			   	 	</div>
			   		<div id="avatar">
				     	<div id="upload">
				 			<span id="span" class="loadButton" imgName="'.$imgName.'">Изменить</span>
				 		</div>
				 		<div id="preview">
		   	 				<img src="'.$avatar.'" />
		   	 				<input type="hidden" name="imgName" value="'.$_POST['imgName'].'" />
		   	 				<span id="status"></span>
				    	</div>
			   	 	</div>
		   	 	</div>
		   	 	<div class="myform">
			   	 	<div class="signature">
			   	 		Название:
			   	 	</div>
			   		<div id="name" class="edit" value="'.$this->row['name'].'">
			   			'.$this->row['name'].'<br /><but class="but">изменить</but>
			   		</div>
			   		<div class="signature">
			   	 		Текст:
			   	 	</div>
			   		<div id="text" class="edit" value="'.$this->row['stext'].'">
			   			'.$this->row['stext'].'<br /><but class="but">изменить</but>
			   		</div>
			   		<div class="citiesBox">
			   		<div class="signature">
			   	 		Отметьте города, в которых будут сохранены изменения:
			   	 	</div>
			   	 	<div class="cityCheck">
			   	 		<div id="allCities">
			   	 			Все города сети<input name="allCities" class="allCities" type="checkbox" value="1" checked="checked">
			   	 		</div>
			   	 		<div id="showCities">
			   	 			Выбрать города
			   	 		</div>
			   	 		<div id="cities">
			   	 			<table>
			   	 	');
					   	 	$cities=explode('|', $this->row['cities']);
					   	 	foreach($cities as $key=>$city)
					   	 	{
					   	 		$cityEls=explode('^', $city);
					   	 		$c=$cityEls[0];
					   	 		$id=$cityEls[1];
					   	 		echo ('
					   	 		<tr>
					   	 			<td width="170">
					   	 			'.$cityRus->city_rus_im($c).'
					   	 			</td>
					   	 			<td width="50">
					   	 				<input name="cityId[]" class="cityCheckBox" type="checkbox" value="'.$c.'^'.$id.'" checked="checked">
									</td>
								</tr>
					   	 		');
							}
							echo ('
							</table>
			   	 		</div>
			   	 	</div>
			   	 	</div>
				</div>
                <input type="submit" value="Сохранить" class="button button-blue" name="editNews">
                <input type="submit" value="Удалить" class="button button-blue" name="deleteNews" id="deleteNews">
                <a href="netscabinet.php?city='.$_GET['city'].'&id='.$this->row['nets_id'].'" class="button button-blue">Отменить</a>
		   </form>

		  </div>
		');
	}
}

$aPage=new aFirmPage($warning, $row, $r);

?>